package com.wcy.entity.pageVo;

import com.wcy.annotation.pageVo.PageColumn;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

//表结构构造器
@Data
public class ColumnConstructor{
    private List<Column> result;
    public ColumnConstructor() {
        result = new ArrayList<>();
    }

    public ColumnConstructor add(PageColumn column){
        Column col = getColumnByProp(column.prop());
        if(col!=null){
            result.remove(col);
        }
        if(!column.display())
            return this;
        result.add(new Column(column));
        return this;
    }

    public ColumnConstructor add(Column column){
        Column col = getColumnByProp(column.getProp());
        if(col!=null){
            col.setLabel(column.getProp());
            col.setType(column.getType());
            col.setAllowEdit(column.getAllowEdit());
        }
        result.add(column);
        return this;
    }

    public ColumnConstructor add(String prop, String label, String type, Boolean allowEdit){
        Column column = getColumnByProp(prop);
        if(column!=null){
            column.setLabel(label);
            column.setType(type);
            column.setAllowEdit(allowEdit);
        }
        result.add(new Column(prop,label,type,allowEdit));
        return this;
    }

    public ColumnConstructor merge(ColumnConstructor additional){
        for (Column column : additional.result) {
            Column oldColumn = getColumnByProp(column.getProp());
            if(oldColumn!=null){
                oldColumn.setLabel(column.getLabel());
                oldColumn.setType(column.getType());
                oldColumn.setAllowEdit(column.getAllowEdit());
            }else{
                result.add(column);
            }
        }
        return this;
    }

    public Column getColumnByProp(String prop){
        for (Column column : result) {
            if (prop.equals(column.getProp())) {
                return column;
            }
        }
        return null;
    }

    public static ColumnConstructor create(){
        return new ColumnConstructor();
    }
}